package cn.itsource.aigou.service.impl;

import cn.itsource.aigou.domain.Brand;
import cn.itsource.aigou.mapper.BrandMapper;
import cn.itsource.aigou.query.BrandQuery;
import cn.itsource.aigou.service.IBrandService;
import cn.itsource.aigou.util.PageList;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 品牌信息 服务实现类
 * </p>
 *
 * @author solargen
 * @since 2019-08-19
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements IBrandService {

    /**
     * 分页高级查询
     * @param query
     * @return
     */
    @Override
    public PageList<Brand> queryPage(BrandQuery query) {
        IPage<Brand> iPage = baseMapper.selectPage(new Page<>(query.getPage(), query.getRows()), query);
        return new PageList<>(iPage.getTotal(),iPage.getRecords());
    }

    /**
     * 加载品牌信息
     * @param productTypeId
     */
    @Override
    public Map<String,Object> loadBrand(Long productTypeId) {
        Map<String,Object> map = new HashMap<>();
        List<Brand> brands = baseMapper.selectList(new QueryWrapper<Brand>().eq("product_type_id", productTypeId));
        map.put("brands", brands);
        Set<String> letters = new TreeSet<>();//有序+去重
        //首字母
        for (Brand brand : brands) {
            letters.add(brand.getFirstLetter());
        }
        map.put("letters", letters);
        return map;
    }
}


class Student implements Comparable<Student>{

    private Long id;
    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    public Student() {
    }

    public Student(Long id, String name) {
        this.id = id;
        this.name = name;
    }


    @Override
    public int compareTo(Student o) {
        System.out.println("比较了一次");
        return (int) (this.getId()-o.getId());
    }


    public static void main(String[] args) {

       /* Set<Student> set = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return (int) (o1.getId()-o2.getId());
            }
        });*/
        Set<Student> set = new TreeSet<>();
        set.add(new Student(1L,"刘伟"));
        set.add(new Student(1L,"吴朝安"));

        System.out.println(set);

    }
}


















